<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :default-sort = "{prop: 'id', order: 'descending'}"
    >
      <el-table-column align="center" label="序号" prop="id"></el-table-column>
      <el-table-column align="center" label="姓名" prop="name"></el-table-column>
      <el-table-column align="center" label="性别" prop="sex"></el-table-column>
      <el-table-column align="center" label="家乡" prop="city"></el-table-column>
      <el-table-column align="center" label="学历" prop="edu"></el-table-column>
      <el-table-column align="center" label="职位" prop="job"></el-table-column>
      <el-table-column align="center" :formatter="ageFormat" label="年龄" prop="birthday"></el-table-column>
      <el-table-column align="center" label="添加时间" prop="ctime"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="update(scope.row)" size="small">修改</el-button>
          <el-button @click="del(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px;text-align: center;align-items: center">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-count="pages"
        v-model:current-page="currpage"
        @current-change="loadData"/>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      currpage:1,
      list: [],
      listLoading: false,
      pages:1
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    ageFormat(row, column) {
      const d1=new Date();
      console.log(row[column.property])
      const d2=new Date(Date.parse(row[column.property].replace(/-/g,"/")));
      return d1.getFullYear()-d2.getFullYear();
    },
    loadData(cp=1){
      this.currpage=cp;
      //请求接口 实现数据的查询
      this.listLoading = true
      request({
        url:"http://127.0.0.1:8080/api/emp/page.do?size=1&page="+this.currpage
      }).then(res=>{
        this.list=res.data.list;
        this.pages=res.data.pages;
        this.listLoading=false;
      })
    },
    del(id){
      this.$confirm('亲，是否确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //请求接口 实现数据的删除
        request({
          url:"http://127.0.0.1:8080/api/emp/del.do?id="+id
        }).then(res=>{
          if(res.code==0){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.loadData();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    update(obj){
      //设置Vuex的数据
      this.$store.commit("user/setEmp",obj);
      //跳转页面 路由
      this.$router.push("/empupdate");
    }
  }
}
</script>
